package com.lyc.service.impl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Service
public class FileUploadService {
    
    @Value("${upload.path}")
    private String uploadPath;
    
    public String uploadToLocal(MultipartFile file) throws IOException {
        // 确保目录存在
        File uploadDir = new File(uploadPath);
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }
        
        // 生成唯一文件名并保存
        String originalFilename = file.getOriginalFilename();
        String fileName = UUID.randomUUID().toString() +
                         originalFilename.substring(originalFilename.lastIndexOf("."));
        
        String filePath = uploadPath + fileName;
        file.transferTo(new File(filePath));
        
        return filePath;
    }
}
